package com.lmz.project.redis.arithmetic.heapShed;


/**
 * 链表 (单双链表) 链表反转
 */
public class LinkTest {

    public static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }

        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public static class DoubleListNode {
        public int value;
        public DoubleListNode next;
        public DoubleListNode last;

        public DoubleListNode(int val) {
            this.value = val;
        }

        public DoubleListNode(int value, DoubleListNode next, DoubleListNode last) {
            this.value = value;
            this.last = last;
            this.next = next;
        }
    }


    class Solution {

        //链表反转
        //测试链接: https://leetcode.cn/problems/reverse-linked-list
        public static ListNode reverseList(ListNode head) {
            ListNode pre = null;
            ListNode next = null;
            while (head != null) {
                next = head.next;
                head.next = pre;
                pre = head;
                head = next;
            }
            return pre;
        }


        public static DoubleListNode reverseList(DoubleListNode head) {
            DoubleListNode pre = null;
            DoubleListNode next = null;
            while (head != null) {
                next = head.next;
                head.next = pre;
                head.last = next;
                pre = head;
                head = next;
            }
            return pre;
        }

    }

}
